package com.sls.nzdapplication.db.utils;

import android.content.Context;

import com.alibaba.fastjson.JSONArray;
import com.sls.nzdapplication.db.DaoManager;
import com.sls.nzdapplication.db.DaoSession;
import com.sls.nzdapplication.db.StationAreaDao;
import com.sls.nzdapplication.model.StationArea;
import com.sls.nzdapplication.app.MySharedPreferences;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StationAreaUtil {
    private static final boolean DUBUG = true;
    private DaoManager manager;
    private com.sls.nzdapplication.model.StationArea StationArea;
    private DaoSession daoSession;
    private MySharedPreferences mySharedPreferences;
    private String username = "";

    public StationAreaUtil(Context context){
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
        mySharedPreferences = new MySharedPreferences(context);
        username = mySharedPreferences.getUserValue("username");
    }

    /**
     * 添加数据，如果有重复则覆盖
     */
    public void insertStationArea(StationArea stationArea) {
        stationArea.setCreateBy(username);
        manager.getDaoSession().insertOrReplace(stationArea);
    }


    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultStationArea(final List<StationArea> stationAreas) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (StationArea stationArea : stationAreas) {
                    stationArea.setCreateBy(username);
                    manager.getDaoSession().insertOrReplace(stationArea);
                }
            }
        });
    }

    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultStationArea2(final List<StationArea> stationAreas) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (StationArea stationArea : stationAreas) {
                    stationArea.setCreateBy(username);
                    manager.getDaoSession().insertOrReplace(stationArea);
                }
            }
        });
    }

    /**
     * 删除数据
     */
    public void deleteStationArea(StationArea stationArea) {
        manager.getDaoSession().delete(stationArea);
    }

    /**
     * 删除全部数据
     */
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 更新数据
     */
    public void updateStationArea(StationArea stationArea) {
        stationArea.setModifyBy(username);
        stationArea.setModifyTime(new Date());
        manager.getDaoSession().update(stationArea);
    }

    /**
     * 按照主键返回单条数据
     */
    public StationArea listOneStationArea(String key) {
        return manager.getDaoSession().load(StationArea.class, key);
    }



    public StationArea queryStationAreaByAreaName(String name){
        QueryBuilder<StationArea> builder = manager.getDaoSession().queryBuilder(StationArea.class);
        StationArea stationArea = builder.where(StationAreaDao.Properties.Name.eq(name)).unique();
        return  stationArea;
    }

    public List<StationArea> queryStationAreaByStatue(String statue){
        QueryBuilder<StationArea> builder = manager.getDaoSession().queryBuilder(StationArea.class);
        List<StationArea> stationArea = builder.where(builder.or(StationAreaDao.Properties.Statue.eq(statue),StationAreaDao.Properties.Statue.eq("start"))).list();
        return  stationArea;
    }

    public List<StationArea> queryStationAreaByUpdateType(String type){
        QueryBuilder<StationArea> builder = manager.getDaoSession().queryBuilder(StationArea.class);
        List<StationArea> stationArea = builder.where(StationAreaDao.Properties.UpdateType.eq(type)).list();
        return  stationArea;
    }


    /**
     * 查询全部数据
     */
    public List<StationArea> queryAll() {
        return manager.getDaoSession().loadAll(StationArea.class);
    }

    public JSONArray queryAllToJson(){
        JSONArray jsonArray = new JSONArray();
        List<StationArea> stationAreas = queryAll();
        for(StationArea stationArea :stationAreas){
            jsonArray.add(stationArea.toJson());
        }
        return jsonArray;
    }

    public void insertByJsArray(JSONArray jsonArray){
        List<StationArea> list = new ArrayList<>();
        for(int i = 0;i<jsonArray.size();i++){
            list.add(new StationArea(jsonArray.getJSONObject(i)));
        }
        insertMultStationArea2(list);
    }
}
